package com.soxing.braindata.web.controller.research;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soxing.braindata.dao.domain.request.ExperimentSearchRequest;
import com.soxing.braindata.dao.entity.Experiment;
import com.soxing.braindata.dao.service.IExperimentService;
import com.soxing.braindata.service.manager.IExperimentManager;
import com.soxing.common.utils.response.MyPage;
import com.soxing.common.utils.response.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :durango
 * @date :2022/7/2 20:05
 * @description 课题实验控制器
 */
@Slf4j
@RestController
@RequestMapping("/research/experiment")
@Api(value = "课题实验管理", tags = {"课题实验管理"})
public class ResearchExperimentController {

    @Resource
    private IExperimentManager experimentManager;
    @Resource
    private IExperimentService experimentService;

    @PostMapping
    @ApiOperation(value = "新增实验")
    public WebResponse<Boolean> add(@RequestBody Experiment experiment) {
        return experimentManager.add(experiment);
    }

    /**
     * 更新实验
     *
     * @param experiment
     * @return 返回操作结果 true:成功 false:失败
     */
    @PutMapping
    @ApiOperation(value = "更新实验")
    public WebResponse<Boolean> update(Experiment experiment) {
        if (experiment.getId() == null) {
            return WebResponse.fail("主键ID为空");
        }
        return experimentManager.update(experiment);
    }

    /**
     * 基于id获取实验信息
     *
     * @param id
     * @return 返回实验信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "基于id获取实验信息")
    public WebResponse<Experiment> getById(@PathVariable("id") Integer id) {
        return experimentManager.getById(id);
    }

    /**
     * 基于id删除实验记录
     *
     * @param id 主键ID
     * @return 返回操作结果 true:成功 false:失败
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "基于id删除实验记录")
    public WebResponse<Boolean> deleteById(@PathVariable("id") Integer id) {
        return experimentManager.deleteById(id);
    }

    /**
     * 基于条件查询实验
     *
     * @param experimentSearchRequest
     * @return
     */
    @PostMapping("/search")
    @ApiOperation(value = "基于条件查询实验")
    public WebResponse<MyPage<Experiment>> search(@RequestBody(required = false) ExperimentSearchRequest experimentSearchRequest) {
        if (experimentSearchRequest == null) {
            experimentSearchRequest = new ExperimentSearchRequest();
        }
        experimentSearchRequest.setPageSize(1000);
        return experimentManager.search(experimentSearchRequest);
    }

    /**
     * 获取用户所在站点的所有设备
     *
     * @return
     */
    @GetMapping("/getAllExperiment")
    @ApiOperation(value = "获取课题所有实验")
    public WebResponse<List<Experiment>> getAllExperiment(@RequestParam String subjectPkId) {
        List<Experiment> list = experimentService.list(new LambdaQueryWrapper<Experiment>().eq(Experiment::getSubjectPkId, subjectPkId));
        return WebResponse.success(list);
    }
}
